<template>
<div class="login-page">
 <el-card>
    <template v-slot:header>
      <span>黑马面经运营后台</span>
    </template>
    <!-- 表单 -->
    <el-form ref="form" :model="form" :rules='rules'>
      <!-- 表单域 -->
      <el-form-item label="用户名" prop='username'>
        <el-input placeholder="请输入用户名：" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop='password'>
        <el-input placeholder="请输入密码：" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item class="tc">
        <!-- v-loading -->
        <el-button type="primary" @click="login" :loading='loading'>登录</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script>
// 引入
import { loginApi } from '@/api/user'
// import { mapMutations } from 'vuex'
export default {
  name: 'HmElementPcIndex',

  data () {
    return {
      form: {},
      // 定义规则
      // rules: {
      //   username: [
      //     { required: true, message: '用户名必填', trigger: 'blur' },
      //     // 最大值最小值
      //     { min: 5, max: 11, message: '用户名必填', trigger: 'blur' }
      //   ],
      //   password: [
      //     { required: true, message: '用户名必填', trigger: 'blur' },
      //     { pattren: /^\w{5,11}$/, message: '用户名必填', trigger: 'blur' }
      //   ]
      // }
      rules: {
        username: [
          { required: true, message: '用户名必填', trigger: 'blur' },
          { min: 5, max: 11, message: '用户名必填', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '用户名必填', trigger: 'blur' },
          { pattren: /^\w{5,11}$/, message: '用户名必填', trigger: 'blur' }

        ]
      },
      loading: false

    }
  },

  mounted () {},

  methods: {
    // ...mapMutations('user', ['updateToken']),
    login () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          console.log(valid)
          try {
            this.loading = true
            // 向后端发送登录请求
            const res = await loginApi(this.form)
            console.log(res)
            // 取token
            this.$store.commit('user/updateToken', res.data.token)
            // this.updateToken()

            this.$router.push('/')
          } finally {
            // 发送完请求就关闭
            this.loading = false
          }
        }
      })
    },
    reset () {
      // form提供的重置
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
// scoped 让组件样式私有化，只能影响到组件根元素，不能渗透到里层
// 如果想要影响到子元素 就需要穿透
// scss穿透语法：::v-deep
// less穿透语法：/deep/

.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  // flex 实现水平垂直居中
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
